package com.sjyp.cms.controller.admin;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.sjyp.cms.model.User;

import java.util.Date;

/**
 * 用户管理
 */
public class UserController extends BaseController {
    public void index() {
        int pageNumber = this.getParaToInt("pageNumber", 1);
        int pageSize = this.getParaToInt("pageSize", 10);
        String sql = "SELECT " +
                "t_user.id, " +
                "t_user.userNo, " +
                "t_user.userName, " +
                "t_user.`password`, " +
                "t_user.userRealName, " +
                "t_user.failCount, " +
                "t_user.`enable`, " +
                "t_user.email, " +
                "t_user.icon, " +
                "t_user.createDate ";
        String sqlExceptSelect = "FROM t_user";
        Page<User> users = User.dao.paginate(pageNumber, pageSize, sql, sqlExceptSelect);
        this.setAttr("users", users);
        this.render("user_list.html");
    }

    public void save() {
        User user = this.getBean(User.class,"");
        user.setCreateDate(new Date());
        boolean isSucess = false;
        if (null == user.getId()) {
            isSucess = user.save();
        } else {
            isSucess = user.update();
        }
        //保存（或修改）后跳转至通用成功（或失败）页面
        this.setCommon("用户","user",isSucess);
    }

    public void goSave() {
        Integer id = this.getParaToInt("id",null);
        if (null != id) {
            User user = User.dao.findById(id);
            this.setAttr("user", user);
        }
        this.render("user_form.html");
    }

    public void del() {
        Integer id = this.getParaToInt("id",null);
        boolean isDelSuccess = User.dao.deleteById(id);
        this.index();
    }
}
